import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import store from "@/store";
import { showNotify } from "vant";

const constantRoutes: RouteRecordRaw[] = [
    // {
    //     path: "/redirect",
    //     component: () => import("../layout/index.vue"),
    //     children: [
    //         {
    //             path: "/redirect/:path(.*)",
    //             component: () => import("../layout/components/Router/Redirect.vue")
    //         }
    //     ]
    // },
    {
        path: "/",
        redirect: "/my",
        name: "App",
    },
    {
        path: "/login",
        component: () => import("../views/login/index.vue")
    },
    {
        path: "/my",
        name: "My",
        meta: {
            isKeepAlive: true
        },
        component: () => import("../views/my/index.vue")
    },
    {
        path: "/my/setting",
        meta: {
            isKeepAlive: true
        },
        component: () => import("../views/my/setting.vue")
    },
    {
        path: "/my/about",
        meta: {
            isKeepAlive: true
        },
        component: () => import("../views/my/about.vue")
    },
    {
        path: "/my/pwd",
        meta: {
            isKeepAlive: true
        },
        component: () => import("../views/my/pwd.vue")
    },
    {
        path: "/serve",
        component: () => import("../views/serve/index.vue")
    },
    {
        path: "/classify",
        component: () => import("../views/classify/index.vue")
    },
    {
        path: "/serve/publicity",
        meta: {
            isKeepAlive: true
        },
        component: () => import("../views/serve/publicity/index.vue")
    },
    {
        path: "/serve/publicity/edit/:id",
        name: "PublicityEdit",
        component: () => import("../views/serve/publicity/edit.vue")
    },
    {
        path: "/serve/publicity/add",
        name: "PublicityAdd",
        component: () => import("../views/serve/publicity/add.vue")
    },
    {
        path: "/404",
        component: () => import("../views/404.vue")
    },
    {
        path: "/:pathMatch(.*)*",
        name: "404",
        component: () => import("@/views/404.vue"),
    },
];

export const router = createRouter({
    history: createWebHashHistory(),
    routes: constantRoutes
});

// 路由拦截白名单
const ignoreToken = [
    "/home",
    "/404",
    '/login',
    '/my/about'
];

router.beforeEach((to, from, next) => {
    console.log(from)
    if (ignoreToken.includes(to.path)) {
        // 白名单可以往下进行
        return next();
    } else {
        // 获取 token
        const token = store.userStore.token;
        // token 不存在
        if (token == null || token === '') {
            showNotify({ type: 'warning', message: '您还没有登录，请先登录' });
            next('/login');
        } else {
            next();
        }
    }
});
